package com.probiz.estoremf.customer.web.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.model.customer.Consult;
import com.probiz.estore.common.model.customer.ConsultAndVoteVO;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.system.AppUser;
import com.probiz.estore.core.controller.GenericStoreFrontController;
import com.probiz.estore.core.event.EventUtil;
import com.probiz.estore.core.event.StoreFrontCache4ProductClearEvent;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.customer.model.ConsultSearchCriteria;
import com.probiz.estore.customer.service.ConsultManager;
import com.probiz.estore.customer.service.ConsultVoteManager;
import com.probiz.estore.product.service.ProdMainManager;
import com.probiz.estore.product.service.ProductManager;
import com.probiz.estore.util.ipwry.IPSeeker;
import com.probiz.estore.webapp.util.RequestContext;
import com.probiz.estore.webapp.util.RequestUtil;

public class ConsultFrontController extends		GenericStoreFrontController<Product> {
	private ProductManager	productManager;
	private ProdMainManager	prodMainManager;
	private ConsultManager consultManager;
	private ConsultVoteManager consultVoteManager;
	protected void initController() throws Exception {
		mgr = productManager;
	}
	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}
	
	public void setProdMainManager(ProdMainManager prodMainManager) {
		this.prodMainManager = prodMainManager;
	}	
	public void setConsultManager(ConsultManager consultManager) {
		this.consultManager = consultManager;
	}
	public void setConsultVoteManager(ConsultVoteManager consultVoteManager) {
		this.consultVoteManager = consultVoteManager;
	}
	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {
		// -----start:获得产品------
		Integer productId = productManager.getProductIdByUri(request.getRequestURI());
		Product product = productManager.getById(productId);
		// 没有找到相应产品或草稿状态的不能访问,直接进入找不到或已删除页面
		if (product == null	|| product.getStatus().shortValue() != Product.STATUS_ACTIVE.shortValue()) {
			try {
				response.sendError(404);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		// -----end:获得产品------

		// -----start:获得商品咨询------
		SearchCriteria searchCriteria = this.createSearchCriteria(request);
		ConsultSearchCriteria consultSearchCriteria = new ConsultSearchCriteria();
		
		bindOnly(request, consultSearchCriteria,"consultSearchCriteria");
		consultSearchCriteria.setProductId(productId);
		consultSearchCriteria.setStatus(Consult.STATUS_SHOW);
		List<Consult>  consultList = consultManager.searchByCriteriaForController(consultSearchCriteria, searchCriteria);
		// -----end:  获得商品咨询------
		
	
		ModelAndView mv = new ModelAndView("customer/consultList");
		mv.addObject("product",product);
		mv.addObject("consultList",consultList);
		mv.addObject("consultSearchCriteria",consultSearchCriteria);
		mv.addObject("count",consultManager.getOnShowConsultCountByProductId(product.getProductId()));

		mv.addObject("currUser",(AppUser) RequestContext.getCurrentUser());
		return mv;
	}
	/**
	 * 对咨询进行投票
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView consultVote(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> data = new HashMap<String, Object>();
		ConsultAndVoteVO consultAndVoteVO = consultVoteManager.doVote(request, RequestUtil.getInteger(request, "consultId"), RequestUtil.getInteger(request, "chosen").shortValue());
		data.put("result", consultAndVoteVO.getResult());
		data.put("consultId", consultAndVoteVO.getConsult().getConsultId());
		data.put("satisfiedCount", consultAndVoteVO.getConsult().getSatisfiedCount());
		data.put("unsatisfiedCount", consultAndVoteVO.getConsult().getUnsatisfiedCount());
		
		//清空该商品页面缓存
		Consult consult = consultManager.getById(RequestUtil.getInteger(request, "consultId"));
		EventUtil.getInstance().fire(new StoreFrontCache4ProductClearEvent(this,consult.getProductId()));
		
		return this.getModelAndAjaxJsonView(data);
	}
	
	/**
	 * 发表咨询
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView addConsult(HttpServletRequest request,HttpServletResponse response){
		//如果评论不允许非匿名登录，且目前没登录，跳转到登录页
		if (ConfigUtil.getInstance().getCustomerConsultRight().intValue() != Consult.CONSULT_RIGHT_ANONYMOUS.intValue() && RequestContext.isAnonymousUser()) {
			return this.getRedirectView("/login.html?fromCheckout=1");
		}
		Product product = productManager.getById(RequestUtil.getInteger(request, "productId"));
		Consult consult = new Consult();
		consult.setProduct(product);
		consult.setMessage(RequestUtil.getParameterNullSafe(request, "message"));
		consult.setCustomerName(request.getParameter("customerName"));
		consult.setCreateTime(new Date());
		consult.setCustomer((AppUser) RequestContext.getCurrentUser());
		consult.setRemoteIp(request.getRemoteAddr());
		consult.setAddress(IPSeeker.getInstance().getAddress(consult.getRemoteIp()));
		consult.setIsRead(Consult.STATUS_UNREAD);
		consult.setSortOrder(Consult.STATUS_UNTOP);
		consult.setStatus(Consult.STATUS_SHOW);
		consult.setSatisfiedCount(Integer.valueOf(0));
		consult.setUnsatisfiedCount(Integer.valueOf(0));
		consult.setReplyCount(Integer.valueOf(0));
		consult.setOnShowReplyCount(Integer.valueOf(0));
		consultManager.addConsult(consult);
		
		//清空该商品页面缓存
		EventUtil.getInstance().fire(new StoreFrontCache4ProductClearEvent(this,consult.getProductId()));
		
		return this.getRedirectView(productManager.getConsultUriByProduct(product));
		
	}
	
	
}
